from abc import ABCMeta, abstractmethod
import  os
verbose = True

class RenameFile:

    def __init__(self,path_src,path_dest):
        self.src,self.dest = path_src,path_dest
    def execute(self):

        if verbose:
            print("文件重命名")
        os.rename(self.src,self.dest)
    def undo(self):
        if verbose:
            print("撤销重命名")
        os.rename(self.dest,self.src)

class CreateFile:
    def __init__(self,path,txt = "hello world!!!"):
        self.path,self.txt = path,txt
    def execute(self):
        if verbose:
            print("正在创建文件")
        with open(self.path,mode='w',encoding="utf-8") as out_file:
            out_file.write(self.txt)
    def undo(self):
        delete_file(self.path)


class ReadFile:
    def __init__(self,path):
        self.path = path
    def execute(self):
        if verbose:
            print("正在读文件")
        with open(self.path,mode="r",encoding='utf-8') as in_file:
            print(in_file.read(),end=" ")

def delete_file(path):
    if verbose:
        print("正在删除文件")
    os.remove(path)




if '__main__' ==__name__:
    origin_name,new_name = 'file1.txt','file2.txt'
    commands=[]



    for cmd in CreateFile(origin_name),ReadFile(origin_name),RenameFile(origin_name,new_name):
        commands.append(cmd)
        print(type(cmd))

    print(commands)
    [c.execute() for c in commands]

    answer  = input("undo ??Y or y")
    if answer not in "Yy":
        print("最后结果是",new_name)
        exit()
    for c in reversed(commands):
        try:
            c.undo()
        except AttributeError as e:
            pass




